/*
https://leetcode.cn/problems/find-all-anagrams-in-a-string/submissions/562533676/
滑动窗口
找到字符串中所有字母异位词
*/

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> result;
        if(s.size()<p.size())return result;
        //if(s==p)return {0};
        unordered_map<char,int> Pmap;
        unordered_map<char,int> Smap;
        for(int i = 0;i < 26;i++){
            Pmap['a'+i] = 0;
            Smap['a'+i] = 0;
        }
        for(int i = 0;i < p.size();i++){
            Pmap[p[i]]++;
            Smap[s[i]]++;
        }
        if(Pmap == Smap){
            result.emplace_back(0);
        }
        for(int i = 0;i < s.size()-p.size();i++){
            Smap[s[i]]--;
            Smap[s[i+p.size()]]++;
            if(Smap == Pmap){
            result.emplace_back(i+1);
            }
        }
        return result;
    }
};